﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-11>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：增加旧欠托收明细
//********************************************************************************//
using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui
{
    public partial class JiuQianTSMX_ZJ : BasePage
    {
        #region 属性

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 记录查询结果集2
        /// </summary>
        private DataTable dtSource2
        {
            get
            {
                return ViewState["dtSource2"] as DataTable;
            }
            set
            {
                ViewState["dtSource2"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();
        BLL.SqlHelper _splHelper = new BLL.SqlHelper();

        /// <summary>
        /// 收费BLL
        /// </summary>
        YS_SF_SHOUFEIBLL _YS_SF_SHOUFEIBLL = new YS_SF_SHOUFEIBLL();

        /// <summary>
        /// 托收明细对象
        /// </summary>
        YS_SF_TUOSHOUMX _YS_SF_TUOSHOUMX = new YS_SF_TUOSHOUMX();

        /// <summary>
        /// 托收明细BLL
        /// </summary>
        YS_SF_TUOSHOUMXBLL _YS_SF_TUOSHOUMXBLL = new YS_SF_TUOSHOUMXBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitailControl();
                LoadData(true);
                this.txtYongHuH.Focus();
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 加载控件
        /// </summary>
        private void InitailControl()
        {
            //获取收费信息
            string shoufeiBH = this.Request["sfbh"];
            //生成新的进账单 else:添加明细
            if (string.IsNullOrEmpty(shoufeiBH))
            {
                this.txtShouFeiBH.Text = "0";
                this.txtZongBiS.Text = "0";
                this.txtZongJinE.Text = "0.00";
            }
            else
            {
                this.txtShouFeiBH.Text = shoufeiBH;
                SetInfo();
                //加载明细列表
                LoadData(true);
            }
            //加载默认数据
            this.txtGuanLiKH.Text = this.Request["guanlikh"];
            this.txtJinE.Text = "0.00";

        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void SetInfo()
        {
            DataRow dr = _YS_SF_SHOUFEIBLL.SelectYS_SF_SHOUFEIsDynamic("i_jlzt=0 and I_SHOUFEIBH=" + this.txtShouFeiBH.Text, null).Rows[0];
            this.txtZongBiS.Text = dr["I_ZONGBIS"].ToString();
            this.txtZongJinE.Text = Convert.ToDecimal(dr["N_ZONGJINE"]).ToString("0.00");
        }

        /// <summary>
        /// 查询GRIDVIEW数据
        /// </summary>
        /// <param name="reGet">是否重新读取数据库</param>
        private void LoadData(bool reGet)
        {
            if (reGet)
            {
                //加载进账单明细
                this.dtSource = _spHelper.P_YSSF_GETTUOSHOUMX(int.Parse(this.txtShouFeiBH.Text)).Tables[0];
            }
            this.gvList1.DataSource = this.dtSource;
            this.gvList1.DataBind();
        }

         /// <summary>
        /// 检查用户号
        /// </summary>
        private bool CheckYongHuH(CallbackResult cr)
        {
            cr.ClearValues();
            YS_YG_YONGHUXXBLL bllTemp = new YS_YG_YONGHUXXBLL();
            DataRowCollection drc = bllTemp.SelectYS_YG_YONGHUXXesDynamic("i_jlzt=0 and i_leixing=0 and s_yonghuh='" + this.txtYongHuH.Text.Trim() + "' and S_GUANLIKBH='" + this.txtGuanLiKH.Text + "'", null).Rows;
            if (drc.Count == 0)
            {
                cr.Msg = "没找到您所选定的用户。";
                cr.IsSuccess = false;
                cr.IsAlertMsg = true;
            }
            else if (Convert.ToInt32(drc[0]["I_SHOUFEIFS"]) != 1)
            {
                cr.Msg = "并非托收用户。";
                cr.IsSuccess = false;
                cr.IsAlertMsg = true;
            }
            else
            {
                cr.IsSuccess = true;
                cr.IsAlertMsg = false;
                cr.AddValue(drc[0]["S_YONGHUM"], drc[0]["S_DIZHI"], drc[0]["S_KAIHUHM"], drc[0]["S_YINHANGZH"], drc[0]["S_KAIHUYH"]);
            }
            return cr.IsSuccess;
        }

        /// <summary>
        /// 检查水量年月
        /// </summary>
        private bool CheckYM(CallbackResult cr)
        {
            cr.ClearValues();
            //判断是否可以增加
            DataRowCollection drc = _spHelper.P_YSZW_GETYINGYEZ(this.txtYongHuH.Text, int.Parse(this.txtYM.Text) / 100, int.Parse(this.txtYM.Text) % 100).Tables[0].Rows;
            if (drc.Count == 0)
            {
                cr.IsSuccess = false;
                cr.IsAlertMsg = true;
                cr.Msg = "该用户当前年月没有加价账单。";
            }
            else if (Convert.ToDecimal(drc[0]["N_YINGSHOUJE"]) <= Convert.ToDecimal(drc[0]["N_SHISHOUJE"]))
            {
                cr.IsSuccess = false;
                cr.IsAlertMsg = true;
                cr.Msg = "该用户指定的水量年月没有欠费,不能重托！";
            }
            else
            {
                cr.IsSuccess = true;
                cr.IsAlertMsg = false;
                cr.AddValue(Convert.ToDecimal(drc[0]["N_YINGSHOUJE"]) - Convert.ToDecimal(drc[0]["N_SHISHOUJE"]));
            }
            return cr.IsSuccess;
        }

        /// <summary>
        /// 检查是否有进行中的计划变更、表计因素注销、计划指标注销来函流程
        /// </summary>
        private bool CheckLaiHan(CallbackResult cr)
        {
            DataRowCollection drc = _spHelper.P_YSSF_HASLAIHAN(this.txtYongHuH.Text.Trim(), int.Parse(this.txtYM.Text.Trim()) / 100, int.Parse(this.txtYM.Text.Trim()) % 100).Tables[0].Rows;
            if (drc.Count > 0)
            {
                cr.IsSuccess = false;
                cr.IsAlertMsg = true;
                cr.Msg = "该用户有正在进行中的相关来函，来函编号：" + drc[0]["S_LAIHANBH"].ToString();
            }
            else
            {
                cr.IsSuccess = true;
                cr.IsAlertMsg = false;
            }
            return cr.IsSuccess;
        }

        /// <summary>
        /// 增加明细
        /// </summary>
        private bool Add(CallbackResult cr)
        {
            cr.ClearValues();
            //判断是否可以增加
            DataRowCollection drc = _spHelper.P_YSZW_GETYINGYEZ(this.txtYongHuH.Text, int.Parse(this.txtYM.Text) / 100, int.Parse(this.txtYM.Text) % 100).Tables[0].Rows;
            if (Convert.ToDecimal(drc[0]["N_SHISHOUJE"]) + Convert.ToDecimal(this.txtJinE.Text) > Convert.ToDecimal(drc[0]["N_YINGSHOUJE"]))
            {
                cr.IsSuccess = false;
                cr.IsAlertMsg = true;
                cr.Msg = "用户已经托收金额与当前托收金额之和大于加价金额。";
                return cr.IsSuccess;
            }
            //判断是否有正在托收未到账的
            DataRowCollection drcTemp = _spHelper.P_YSSF_GETTUOSHOUYMEXISTS(this.txtYongHuH.Text, int.Parse(this.txtYM.Text)).Tables[0].Rows;
            if (drcTemp.Count > 0)
            {
                cr.IsSuccess = false;
                cr.IsAlertMsg = true;
                cr.Msg = "该笔费用正在托收,但尚未到账,不允许再次进行重托！";
                return cr.IsSuccess;
            }
            // 判断进账单是否可添加
            int V_Err = 0;
            string V_Msg;
            _splHelper.P_YSSF_CHECKJQTUOSHOUJZD(this.txtYongHuH.Text, int.Parse(this.txtYM.Text), int.Parse(this.txtShouFeiBH.Text), out V_Err, out V_Msg);
            if (0 != V_Err)
            {
                cr.IsSuccess = false;
                cr.IsAlertMsg = true;
                cr.Msg = V_Msg;
                return cr.IsSuccess;
            }
            //执行操作
            //获取站点
            DataRow drZD = new YS_JX_GUANLIKBLL().SelectYS_JX_GUANLIKsDynamic("S_GUANLIKH='" + this.txtGuanLiKH.Text + "'", null).Rows[0];
            this._YS_SF_TUOSHOUMX.S_ZHANDIANBH = drZD["S_ZHANDIAN"].ToString();
            this._YS_SF_TUOSHOUMX.I_FEIYONGBH = Convert.ToInt32(drc[0]["I_FEIYONGBH"]);
            //如果收费编号为0,则是生成新的进账单,需要读取当前最大的收费编号
            if (this.txtShouFeiBH.Text == "0")
            {
                this._YS_SF_TUOSHOUMX.I_HEZHANGBH = int.Parse(BusinessTool.GETMAXVALUE("YS_SF_SHOUFEI", "I_SHOUFEIBH", null)) + 1;
            }
            else
            {
                this._YS_SF_TUOSHOUMX.I_HEZHANGBH = int.Parse(this.txtShouFeiBH.Text);
            }
            //获取当前托收凭证号
            this._YS_SF_TUOSHOUMX.I_TUOSHOUPZH = int.Parse(BusinessTool.GETMAXVALUE("YS_SF_TUOSHOUMX", "I_TUOSHOUPZH", null)) + 1;
            this._YS_SF_TUOSHOUMX.I_JLZT = 0;
            this._YS_SF_TUOSHOUMX.I_M = int.Parse(this.txtYM.Text) % 100;
            this._YS_SF_TUOSHOUMX.I_PRINT = 0;
            this._YS_SF_TUOSHOUMX.I_TICHUBZ = 0;
            this._YS_SF_TUOSHOUMX.I_TUOSHOULX = 2;
            this._YS_SF_TUOSHOUMX.I_Y = int.Parse(this.txtYM.Text) / 100;
            this._YS_SF_TUOSHOUMX.N_JINE = double.Parse(this.txtJinE.Text);
            this._YS_SF_TUOSHOUMX.S_GUANLIKBH = this.txtGuanLiKH.Text;
            this._YS_SF_TUOSHOUMX.S_KAIHUHM = this.txtKaiHuHM.Text;
            this._YS_SF_TUOSHOUMX.S_KAIHUYH = this.txtKaiHuYH.Text;
            this._YS_SF_TUOSHOUMX.S_KAIHUZH = this.txtKaiHuZH.Text;
            this._YS_SF_TUOSHOUMX.S_YONGHUH = this.txtYongHuH.Text;
            this._YS_SF_TUOSHOUMX.S_YONGHUM = this.txtYongHuM.Text;
            this._YS_SF_TUOSHOUMX.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            this._YS_SF_TUOSHOUMX.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            _SYS_LOGBLL.Log<YS_SF_TUOSHOUMX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "增加托收明细", OperateState.Add, this._YS_SF_TUOSHOUMX, null);
            if (this._YS_SF_TUOSHOUMXBLL.InsertOrUpdate(this._YS_SF_TUOSHOUMX))
            {
                
                //更新已托收金额
                string rsNo = "-1";
                string rsMsg = "";
                _spHelper.P_YSSF_UPDATETUOSHOUJE((int)this._YS_SF_TUOSHOUMX.I_HEZHANGBH, out rsNo, out rsMsg);
                if (rsNo == "1")
                {
                    cr.IsSuccess = true;
                    cr.Msg = "操作成功！";
                    cr.IsAlertMsg = false;
                    //更新银行代理汇总明细等信息
                    _spHelper.P_YSSF_GETTUOSHOUSCQK((int)this._YS_SF_TUOSHOUMX.I_HEZHANGBH, 0, out rsNo, out rsMsg);
                    _spHelper.P_YSSF_GETTUOSHOUSCQK((int)this._YS_SF_TUOSHOUMX.I_HEZHANGBH, 1, out rsNo, out rsMsg);
                    DataRow dr = _YS_SF_SHOUFEIBLL.SelectYS_SF_SHOUFEIsDynamic("i_jlzt=0 and I_SHOUFEIBH=" + this._YS_SF_TUOSHOUMX.I_HEZHANGBH, null).Rows[0];
                    cr.AddValue(this._YS_SF_TUOSHOUMX.I_HEZHANGBH, dr["I_ZONGBIS"].ToString(), Convert.ToDecimal(dr["N_ZONGJINE"]).ToString("0.00"));
                }
            }
            return cr.IsSuccess;
        }

        #endregion

        /// <summary>
        /// 列表刷新
        /// </summary>
        protected void gvList1_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData(true);
        }

        #region 事件

        /// <summary>
        /// 用户号回车查询事件
        /// </summary>
        protected void btnLoadYongHuXX_Click(object sender, EventArgs e)
        {
            if (this.txtYongHuH.Text.Trim().Length == 7)
            {

            }
            else
            {
                PageTools.AlertMsg(this, "用户号应为7位。");
                this.txtYongHuH.Focus();
            }
        }

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            if (e.Parameter == "yonghuh")
            {
                this.CheckYongHuH(cr);
            }
            else if (e.Parameter == "ym")
            {
                this.CheckYM(cr);
            }
            else if (e.Parameter == "add")
            {
                if (this.CheckYongHuH(cr))
                {
                    if (this.CheckYM(cr))
                    {
                        //计划变更、表计因素注销来涵与旧欠托收互斥 20190322 add 
                        if (this.CheckLaiHan(cr))
                        {
                            this.Add(cr);
                        }
                    }
                }
            }
            e.Result = cr.ToString();
        }

        #endregion
    }
}